<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class Group_model extends Model {


    public function __construct($params = array())
    {
        // Call the Model constructor
        parent::__construct();
        $this->config->load('group');
        log_message('debug', "Group_model Model Initialized");
    }


    function setId($id)
    {
        $this->_userId = $id;
    }

    function setGroupId($id)
    {
        $this->_groupId = $id;
    }

    function setStatus($status)
    {
        $this->_userStatus = $status;
    }

    public function setUserLocation($location) {
        $this->_userLocation = $location;
    }

    public function setUserLocationAccess($locationAccess) {
        $this->_userLocationAccess = $locationAccess;
    }

    function add($data=array())
    {
        if (! $data) return false;
        $data['groupCreateTime'] = time();
        $data['groupModifiedTime'] = time();
        $result = $this->db->insert('Group',$data) && $this->db->affected_rows();
        return $result;
    }


    function get($id)
    {
        if (! $id) return false;
        $this->db->select('Group.*');
        $this->db->from('Group');
        $this->db->where('groupId',$id);
        $this->db->limit(1);
        $query = $this->db->get();
        if ($query->num_rows() > 0)
            return $query->row_array();
        return false;
    }
    function getByTitle($title) {
        if (empty($title)) return array();
        $this->db->select('Group.*');
        $this->db->from('Group');
        $this->db->where('groupName',$title);
        $query = $this->db->get();
        if ($query->num_rows() > 0)
            return $query->row();
        return array();
    }


    function addUser($id,$groupId)
    {
        if ($this->getUser($id,$groupId)) return false;
        $data = array("userId" => $id, "groupId" => $groupId);
        return $this->db->insert('UserGroup',$data) && $this->db->affected_rows();
    }

    function getUser($id,$groupId)
    {
        if (! $id) return false;
        if (! $groupId) return false;
        $this->db->from('UserGroup');
        $this->db->where('userId',$id);
        $this->db->where('groupId',$groupId);
        $query = $this->db->get();
        if ($query->num_rows() > 0)
            return $query->row();
        return false;

    }

    function del($id)
    {
        if (! $this->get($id)) return false;
        $this->db->where('groupId', $id);
        return ($this->db->delete('Group') && ($this->db->affected_rows() > 0));
    }

    function delUser($id,$groupId)
    {
        if (! $this->getUser($id,$groupId)) return false;
        $this->db->where('userId',$id);
        $this->db->where('groupId',$groupId);
        return $this->db->delete('UserGroup') && $this->db->affected_rows();
    }



    function update($id,$data=array())
    {
        if (! $this->get($id)) return false;
        if (! $data) return false;
        $this->db->where('groupId',$id);
        $result = $this->db->update('Group',$data) && $this->db->affected_rows();
        return $result;
    }

    function getList($limit = false,$offset=0)
    {
        $this->db->select('Group.*', TRUE);
        $this->db->from('Group');
        $this->db->order_by('groupName','ASC');
        $this->db->limit($limit,$offset);
        $query = $this->db->get();
        $list = array();
        if ($query->num_rows() > 0)
            return $query->result_array();
        return $list;
    }

    function getCount()
    {
        $this->db->select('count(groupId) AS count', TRUE);
        $this->db->from('Group');
        $query = $this->db->get();
        if ($query && ($row = $query->row_array ())) {
            return $row ['count'];
        }
        return 0;
    }

    function getListUser($id, $where=array(), $limit = false, $offset=0)
    {
        if (! $this->get($id)) return false;
        $this->db->select('User.*');
        $this->db->from('UserGroup');
        
        //$this->db->join('Group','UserGroup.groupId = Group.groupId');
        $this->db->join('User','User.userId = UserGroup.userId');
        $this->db->join ('UserLocation ul' , 'User.userId = ul.userId' );
        
        $this->db->where('UserGroup.groupId',$id);
        
        if (isset ( $where ['kotamaId'] ))
            $this->db->where ( 'ul.kotamaId' , $where ['kotamaId']);
        if (isset ( $where ['satuanId'] )) 
            $this->db->where ( 'ul.satuanId' , $where ['satuanId']);
        if (isset ( $where ['unitId'] )) 
            $this->db->where ( 'ul.unitId' , $where ['unitId']);
        if (isset ( $where ['bagianId'] )) 
            $this->db->where ( 'ul.bagianId' , $where ['bagianId']);
        if (isset ( $where ['subBagianId'] )) 
            $this->db->where ( 'ul.subBagianId' , $where ['subBagianId']);
        $this->db->order_by('User.userLogin','asc');
        
        if ($limit && $limit > 0)
            $this->db->limit($limit,$offset);
        $query = $this->db->get();
        return $query->result_array();
    }
    
	function getCountUser($id, $where=array())
    {
        if (! $id) 
        	return false;
        	
        $this->db->select('count(groupId) AS count');
        $this->db->from('UserGroup');
        
        //$this->db->join('Group','UserGroup.groupId = Group.groupId');
        $this->db->join('User','User.userId = UserGroup.userId');
        $this->db->join ('UserLocation ul' , 'User.userId = ul.userId' );
        
        $this->db->where('UserGroup.groupId',$id);
        if (isset ( $where ['kotamaId'] ))
            $this->db->where ( 'ul.kotamaId' , $where ['kotamaId']);
        if (isset ( $where ['satuanId'] )) 
            $this->db->where ( 'ul.satuanId' , $where ['satuanId']);
        if (isset ( $where ['unitId'] )) 
            $this->db->where ( 'ul.unitId' , $where ['unitId']);
        if (isset ( $where ['bagianId'] )) 
            $this->db->where ( 'ul.bagianId' , $where ['bagianId']);
        if (isset ( $where ['subBagianId'] )) 
            $this->db->where ( 'ul.subBagianId' , $where ['subBagianId']);
        
        $query = $this->db->get();
        if ($query && ($row = $query->row_array ())) {
            return $row ['count'];
        }
        return 0;
    }

    public function addAccess($id,$access=array())
    {
        if (! $this->get($id)) return false;
        $this->db->trans_begin();
        $this->db->query("DELETE FROM GroupAccess WHERE groupId = ? ", array($id));
        if (is_array($access) && $access)
        foreach ($access AS $key=>$value) {
            $data = array('accessId' => $value, 'groupId' => $id);
            $query = $this->db->insert_string('GroupAccess',$data);
            $this->db->query($query);
        }
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
            return true;
        }
    }

    function getListAccess($id)
    {
        if (! $this->get($id)) return false;
        $this->db->select('GroupAccess.accessId');
        $this->db->from('GroupAccess');
        //members_access.members_access_id = members_access_to_members.members_access_id
        $this->db->join('Access', 'Access.accessId = GroupAccess.accessId');
        $this->db->where('GroupAccess.groupId',$id);
        $query = $this->db->get();
        $list = array();
        if ($query->num_rows() > 0) {
            foreach($query->result() AS $row) {
                $list[] = $row->accessId;
            }
        }
        return $list;
    }
    
	public function getGroupList() {
        $query = $this->db->get ("Group");
        $list = array('all'=>'Semua Group');
        foreach($query->result_array() as $row)
            $list[$row['groupId']] = $row['groupName'];
        return $list;
    }

	public function userList($groupId ,$limit = 100, $offset = 0) {
    	$this->db->select( 'User.userId,User.userLogin,User.userName' );
    	$this->db->from ( 'User' );
        $this->db->where ( 'userId NOT IN (SELECT userId FROM UserGroup WHERE groupId = "'.$groupId.'") ');
        $this->db->order_by( 'userLogin' );
        $query = $this->db->get ();

        return $query->result_array ();
    }
    
    //Rahmat
	public function groupList($parent=0, $level = 0) {
        $list = array ();
        $this->db->select ( '*' );
        $this->db->from ( 'Group' );
        $this->db->where ( 'Group.groupPid', $parent );
        $this->db->order_by ( 'Group.groupName', 'asc' );
        $query = $this->db->get ();
        if (! $query)
            return array ();
        foreach ( $query->result_array () as $row ) {
            $row ['level'] = $level;
            $list [] = $row;
            $child = $this->groupList ( $row ['groupId'], $level + 1 );
            if ($child)
                $list = array_merge ( $list, $child );
        }
   
        return $list;
    } 
    
    public function dropdownList( $parent=0 ) {
        $this->db->select ( 'groupId, groupName' );
        $this->db->from ( 'Group' );
        $this->db->where ( 'groupPid', $parent );
        $this->db->order_by ( 'groupName', 'asc' );
        $query = $this->db->get ();
        return $query->result_array();
    }
    
    public function groupKodeMax($parent=0) {
   		$this->db->select_max ( 'groupKode' );
        $this->db->from ( 'Group' );
        $this->db->where ( 'groupPid', $parent );
        $this->db->limit (1);
        $query = $this->db->get ();
        return $query->row_array();
    }
    
	public function getUserGroup( $userId ) {
        $this->db->select ( 'Group.groupKode' );
        $this->db->from ( 'UserGroup' );
        $this->db->join ( 'Group' , 'UserGroup.groupId = Group.groupId' );
        $this->db->where ( 'UserGroup.userId', $userId );
        $query = $this->db->get ();
        
        $list = '';
        foreach($query->result_array() as $row) {
        	$list[] = $row['groupKode'];
        }
        
        return $list;
    }
    
	public function getDataComplete( $param = '' , $alias = "", $where=array(), $userId ) {
		if ( $alias )
			$alias = " as ".$alias;
		
    	$this->db->select( 'al.userLogin' . $alias );
    	$this->db->distinct( 'userLogin' );
    	
        $this->db->from ( 'User al' );
        $this->db->join ( 'UserLocation ul' , 'al.userId = ul.userId' );
        
        if (isset ( $where ['kotamaId'] ))
            $this->db->where ( 'ul.kotamaId' , $where ['kotamaId']);
        if (isset ( $where ['satuanId'] )) 
            $this->db->where ( 'ul.satuanId' , $where ['satuanId']);
        if (isset ( $where ['unitId'] )) 
            $this->db->where ( 'ul.unitId' , $where ['unitId']);
        if (isset ( $where ['bagianId'] )) 
            $this->db->where ( 'ul.bagianId' , $where ['bagianId']);
        if (isset ( $where ['subBagianId'] )) 
            $this->db->where ( 'ul.subBagianId' , $where ['subBagianId']);
        
        if (isset ( $userId )) 
            $this->db->where ( 'al.userId != ' , $userId);
            
        $this->db->like( 'al.userLogin' , $param, 'both'); 
        $this->db->order_by( 'al.userLogin' , 'asc' );
        $query = $this->db->get ();

        return $query->result_array ();
        
    }
}
